'use strict';

const Service = require('egg').Service;

class HomeService extends Service {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async checkUser(obj) {
    try {
      const userInfo = await this.app.mysql.query('select * from users where name=?', obj.name);
      return {
        code: 1,
        isExists: userInfo.length > 0,
      };
    } catch (err) {
      return {
        code: 0,
        msg: err.message,
      };
    }
  }
  async register(user) {
    try{
      await this.app.mysql.insert('users', user)
      return {
        code: 1,
        msg: '注册成功'
      }
    } catch(e) {
      return {
        code: 0,
        msg: e.message
      }
    }
  }
}

module.exports = HomeService;
